﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//using Evaluation3.utils;
using Evaluation3.services;
using Evaluation3.classes;

public partial class uicontrol_hoidongInfo : System.Web.UI.UserControl
{
    //dataUtils objDataUtils = new dataUtils();
    cucilServices objCucil;

    #region Page_Load()
    protected void Page_Load(object sender, EventArgs e)
    {
        objCucil = new cucilServices();    
        LoadHD();
    }
    #endregion

    #region LoadHD()
    public DataTable LoadHD()
    {
        objCucil.SearchInput = txtSearchIn.Text.Trim();
        objCucil.MaDoanhNghiep = int.Parse(((remoteUser)Session[keyUtils.SES_REMOTEUSER]).companyID);
        DataTable dtHD = objCucil.HD_Search().Tables[0];
        grvHD.DataSource = dtHD;
        grvHD.DataBind();

        lblSearchResult.Text = "Hiện có " + dtHD.Rows.Count + " Hội đồng :";
        return dtHD;
    }
    #endregion

    #region btnSearch_Click()
    protected void btnSearch_Click(object sender, EventArgs e)
    {        
        LoadHD();
        lblNotice.Text = "";
    }
    #endregion

    #region XoaHD_Click()
    protected void XoaHD_Click(object sender, EventArgs e)
    {
        try
        {
            bool chkBox = false;
            int count = 0;
                        
            for (int i = 0; i < grvHD.Rows.Count; i++)
            {
                GridViewRow row = grvHD.Rows[i];
                //Tìm ra Dòng nào đã bị check
                bool isChked = ((CheckBox)row.FindControl("deleteRec")).Checked;
                //Nếu đã Checked thì thực hiện việc xóa
                if (isChked)
                {                   
                    //Cells[1] tức là cột MaHoiDong vi so 0 la cot CheckBox
                    objCucil.MaHoiDong = int.Parse(grvHD.Rows[i].Cells[1].Text);
                    objCucil.HD_Delete();                    
                    count++;
                    chkBox = true;
                }
            }
            lblNotice.Text = "Đã xóa " + count.ToString() + " Hội đồng ra khỏi dữ liệu.";
            LoadHD();
            if (!chkBox) 
            {
                Response.Write("<script>alert('Phải chọn Hội đồng muốn xóa trước khi xóa!');</script>");
            }
        }
        catch (Exception ex)
        {
            lblNotice.Text = ex.ToString();
        }
        lblNotice.Visible = true;
    }
    #endregion
        
    #region grvHD_RowCreated()
    protected void grvHD_RowCreated(object sender, GridViewRowEventArgs e)
    {
        //e.Row.Cells[7].Controls[1].Visible = true;
        //grvHD.Columns[1].Visible = false;
        //e.Row.Cells(3).Visible = false;
        //e.Row.Cells[e.Row.DataItemIndex];
        
        
    }
    #endregion

    #region grvHD_RowSelectToEdit()
    protected void grvHD_RowSelectToEdit(object sender, GridViewSelectEventArgs e)
    {
        //e.ToString();
        Response.Redirect("Render.aspx?mod=hoidongEdit?hdcode=" + e.ToString());
    }
    #endregion        

    #region grvHD_RowCommand()
    protected void grvHD_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        { 
            //Lay ve ID cua Row Clicked
            int selectedrow = Convert.ToInt32(e.CommandArgument);
            string hdcode = grvHD.Rows[selectedrow].Cells[1].Text;
            Session["HDCode"] = hdcode;
            Response.Redirect("adminRender.aspx?mod=hoidongEdit");//?hdcode=" + hdcode);
        }
    }
    #endregion

    #region Giấu đi các Cột không muốn ở các GridView
    protected void grvHD_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) && (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header))
        {
            //Giấu đi cột Mã Hội Đồng
            e.Row.Cells[1].Visible = false;         
        }
        if ((e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) && (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header))
        {
            //Giấu đi cột Mã Thành Viên            
            e.Row.Cells[3].Visible = false;
        }
    }
    #endregion

    #region Phân Trang
    protected void grvHD_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvHD.DataSource = LoadHD();
        grvHD.PageIndex = e.NewPageIndex;
        grvHD.DataBind();
    }
    #endregion
}
